<?php

namespace Controller;

class CommentController {
	/**
	 * @return String $ret 
	 */
	public function doControll(\Model\Database $database, $p_id) {
		$commentView = new \View\CommentView();
		$commentCatalog = new \Model\CommentHandler($database);
		$comments = $commentCatalog -> getAllComments($p_id);

		$message = "";
		$messageid = 0;
		$ret = "";
		//Kollar om användaren har tryckt på submit
		if ($commentView -> TriedToSubmit()) {
			
			$messageid = $commentCatalog -> newComment($p_id, $commentView -> Name(), $commentView -> Comment());

			if ($messageid == \Model\CommentHandler::COMMENT_EMPTY) {
				$message .= $commentView -> NewCommentMess(\View\CommentView::COMMENT_EMPTY);
			}
			if ($messageid == \Model\CommentHandler::COMMENT_SUCCESS) {
				$message .= $commentView -> NewCommentMess(\View\CommentView::COMMENT_SUCCESS);
				header('Location: index.php?postID=' . $p_id);
			}
			if ($messageid == \Model\CommentHandler::COMMENT_FAILED) {
				$message .= $commentView -> NewCommentMess(\View\CommentView::COMMENT_FAILED);
			}
			$ret .= $message;
		}
		//Om användaren trycker på radera
		if ($commentView -> TriedToDelete()) {
			$commentid = $commentView -> CommentID();
			$commentCatalog -> deleteComment($commentid);
			header('Location: index.php?postID=' . $p_id . '#comments');
		}
		
		$ret .= $commentView -> NewCommentForm();
		$ret .= $commentView -> showListOfComments($comments, $database);
		
		return $ret;
	}

}
